﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace BHair.Base
{
    public partial class frmxuqi : Form
    {
        public frmxuqi()
        {
            InitializeComponent();
        }
        string siweils = "";
        string constr = ConfigurationManager.ConnectionStrings["BHair.Properties.Settings.BHairDBConnectionString"].ConnectionString;
        private void frmxuqi_Load(object sender, EventArgs e)
        {
            shoukuanzhanghu();
            djh();
        }
        /// <summary>
        /// 加载收款方式
        /// </summary>
        public void shoukuanzhanghu()
        {
            SqlConnection conn = new SqlConnection(constr);
            try
            {
                conn.Open();
                string sql = "SELECT [zhID],[zhname] FROM [BHairDB].[dbo].[yhzh]";
                SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
                DataSet ds = new DataSet();
                sda.Fill(ds, "yhzh");
                cbozhanghu.DataSource = ds.Tables["yhzh"];
                cbozhanghu.DisplayMember = "zhname";
            }
            catch (Exception ee)
            {
                conn.Close();
                MessageBox.Show(ee.Message);
            }
        }
        /// <summary>
        /// 单据号
        /// </summary>
        public void djh()
        {
            string lsh = "";
            try
            {
                SqlConnection conn = new SqlConnection(constr);
                conn.Open();
                string sql = "SELECT [xuqi] FROM [BHairDB].[dbo].[djh]";
                SqlCommand comm = new SqlCommand(sql, conn);
                SqlDataReader sdr = comm.ExecuteReader();
                while (sdr.Read())
                {
                    lsh = sdr["xuqi"].ToString();
                }
                string bs = "XQ-";
                string time = DateTime.Now.ToString("yyyy-MM-dd");
                /*
                Random ran = new Random();
                int RandKey = ran.Next(10, 999);
                */
                int djlsh = Int32.Parse(lsh) + 1;
                siweils = string.Format("{0:00000}", djlsh);
                string djh = bs + time + "-" + siweils;
                this.txtdjh.Text = djh;
                conn.Close();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (txtcardID.Text != string.Empty)
            {
                chuanzhi ca = new chuanzhi();
                if (ca.CheckPass(txtcardID.Text) == true)
                {
                    string card = txtcardID.Text;
                    string sql = string.Format("SELECT [mid] ,[mname] ,[mstatus] ,[mcid] ,[mpass] ,[mremark] ,[msex] ,[mmonth] ,[mday] ,[mphone] ,[maddress],[mother] ,[mjoin],[midcard],[mbalance],[mpoint],[mlastime],[image] ,[ifyxq] ,[yxq] FROM [BHairDB].[dbo].[member] where mid='{0}'", txtcardID.Text);
                    SqlConnection conn = new SqlConnection(constr);
                    conn.Open();
                    SqlCommand comm = new SqlCommand(sql, conn);
                    SqlDataReader sdr = comm.ExecuteReader();
                    while (sdr.Read())
                    {
                        txtdaoqi.Text = sdr["yxq"].ToString();
                        lbckr.Text = sdr["mname"].ToString();
                    }
                    conn.Close();
                }
                else
                {
                    MessageBox.Show("卡号不存在！");
                }
            }
            else
            {
                MessageBox.Show("请输入卡号！");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (txtcardID.Text != string.Empty)
            {
                insertxuqi();
                insertshouru();
                SqlConnection conn = new SqlConnection(constr);
                conn.Open();
                string sql = string.Format("UPDATE [BHairDB].[dbo].[member]SET [yxq] = '{0}' WHERE mid='{1}'", dtpxuqi.Text, txtcardID.Text);
                SqlCommand comm = new SqlCommand(sql, conn);
                comm.ExecuteNonQuery();
                MessageBox.Show("续期成功");
                djhshuaxin();
                shuaxin();
                djh();
            }
            else
            {
                MessageBox.Show("请正确填写");
            }
        }
        /// <summary>
        /// 插入续期表
        /// </summary>
        public void insertxuqi()
        {
            string sql =string.Format( "INSERT INTO [BHairDB].[dbo].[会员续期] ([单据号] ,[卡号] ,[到期日期] ,[延期时间]  ,[经手人]  ,[单据日期] ,[续费金额] ,[收款账户]) VALUES ('{0}' ,'{1}'  ,'{2}' ,'{3}' ,'{4}' ,'{5}' ,'{6}' ,'{7}')",txtdjh.Text,txtcardID.Text,txtdaoqi.Text,dtpxuqi.Text,chuanzhi.userID, DateTime.Now.ToString("yyyy-MM-dd"),txtss.Text,cbozhanghu.Text);
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            SqlCommand comm = new SqlCommand(sql,conn);
            comm.ExecuteNonQuery();
        }
        /// <summary>
        /// 刷新单据号
        /// </summary>
        public void djhshuaxin()
        {
            try
            {
                SqlConnection conn = new SqlConnection(constr);
                conn.Open();

                string sql = string.Format("UPDATE [BHairDB].[dbo].[djh] SET [xuqi] = '{0}'", siweils);
                SqlCommand comm = new SqlCommand(sql, conn);
                comm.ExecuteNonQuery();
            }
            catch { }
        }
        public void insertshouru()
        {
            try
            {
                string sql = string.Format("INSERT INTO [BHairDB].[dbo].[收入明细] ([djh] ,[skje] ,[zsje] ,[djje] ,[yhje] ,[skfs] ,[djbz] ,[jsr] ,[djrq],[班组]) VALUES ('{0}' ,'{1}' ,'0.00' ,'{2}' ,'0.00','{3}' ,'{4}' ,'{5}','{6}','{7}')", txtdjh.Text, txtss.Text, txtss.Text, cbozhanghu.Text,"续期卡号："+txtcardID.Text+"姓名："+lbckr.Text, chuanzhi.userID, DateTime.Now.ToString("yyyy-MM-dd"),chuanzhi.banzu);
                SqlConnection conn = new SqlConnection(constr);
                conn.Open();
                SqlCommand comm = new SqlCommand(sql, conn);
                comm.ExecuteNonQuery();
                conn.Close();
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
        }
        public void shuaxin()
        {
            txtcardID.Text = "";
            txtdaoqi.Text = "";
            txtss.Text = "0.00";
           // txtys.Text = "0.00";
        }



        private void txtcardID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (txtcardID.Text != string.Empty)
                {
                    chuanzhi ca = new chuanzhi();
                    if (ca.CheckPass(txtcardID.Text) == true)
                    {
                        string card = txtcardID.Text;
                        string sql = string.Format("SELECT [mid] ,[mname] ,[mstatus] ,[mcid] ,[mpass] ,[mremark] ,[msex] ,[mmonth] ,[mday] ,[mphone] ,[maddress],[mother] ,[mjoin],[midcard],[mbalance],[mpoint],[mlastime],[image] ,[ifyxq] ,[yxq] FROM [BHairDB].[dbo].[member] where mid='{0}'", txtcardID.Text);
                        SqlConnection conn = new SqlConnection(constr);
                        conn.Open();
                        SqlCommand comm = new SqlCommand(sql, conn);
                        SqlDataReader sdr = comm.ExecuteReader();
                        while (sdr.Read())
                        {
                            txtdaoqi.Text = sdr["yxq"].ToString();
                        }
                        conn.Close();
                    }
                    else
                    {
                        MessageBox.Show("卡号不存在！");
                    }
                }
                else
                {
                    MessageBox.Show("请输入卡号！");
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
